import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GChild from "../../../Core/GView/GChild";
import { BOXPREVIEW, CHAPTERBOX, CMsg } from "../../Common/Define";
import { L } from "../../Common/Language";
import { VIEW_ID } from "../../Common/UI";
import { Res } from "../../Common/UIResources";
import { JXLocales } from "../../Common/Zh";
import GameMgr from "../../Logic/GameMgr";

const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/BoxItem')

export default class BoxItem extends GChild {

    @property({
        type: cc.Label,
        tooltip: "宝箱所在关卡"
    })
    boxLevel: cc.Label = null;

    @property({
        type: cc.Sprite,
        tooltip: "宝箱图片"
    })
    boxImg: cc.Sprite = null;


    @property({
        type: cc.Node,
        tooltip: "宝箱图片"
    })
    receive: cc.Node = null;

    @property({
        type: cc.Animation,
        tooltip: "宝箱晃动"
    })
    box: cc.Animation = null;


    /**当前所处章节 */
    protected curChapter: number = 0;
    /**当前章节期间已通关关卡 */
    protected levelStr: number = 1;
    /**数据 */
    protected _data: SStoryDataRaw;
    protected _index: number = null;
    /**
     * 初始宝箱
     * @param data 宝箱单个数据
     * @param index 第几关
     * @param curChapter 第几个章节
     * @param curlevel 章节通关关卡数量
     */
    public initBox(data: SStoryDataRaw, index: number, curChapter: number, curlevel: number) {
        this._index = index;
        this.levelStr = curlevel;
        this.curChapter = curChapter;
        this.boxLevel.string = '第' + index + '关';
        this._data = data;
        let level = GameMgr.lUserData.level;
        let chapterBoxArr = GameMgr.lUserData.chapterBoxArr;
        let bool = false;
        if (chapterBoxArr.length > 0) {
            chapterBoxArr.forEach((v) => {
                if (bool) {
                    return;
                }
                if (v == this._data.id) {
                    bool = true;
                    this.boxImg.node.getComponent(cc.Button).interactable = false;
                    this.showImg(CHAPTERBOX.receive);
                } else {
                    if (data.id <= level) {
                        this.showImg(CHAPTERBOX.lock);
                        this.boxImg.node.getComponent(cc.Button).interactable = true;
                    } else if (data.id > level) {
                        this.showImg(CHAPTERBOX.unLock);
                        this.boxImg.node.getComponent(cc.Button).interactable = false;
                    }
                }
            })
        } else {
            if (data.id <= level) {
                this.showImg(CHAPTERBOX.lock);
                this.boxImg.node.getComponent(cc.Button).interactable = true;
            } else if (data.id > level) {
                this.showImg(CHAPTERBOX.unLock);
                this.boxImg.node.getComponent(cc.Button).interactable = false;
            }
        }

    }
    _onTouchStar0(event: cc.Event.EventTouch) {
        // GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.levelBox)
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.previewCtrl, this._data, BOXPREVIEW.LEVELBOX);
    }

    /**渲染宝箱图片 */
    protected showImg(chapte: number) {
        switch (chapte) {
            case CHAPTERBOX.unLock:
                {
                    this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
                    this.assetImpl.spriteAtlasFrame(this.boxImg, Res.texture.views.mainUi, 'UnSecectBox');
                    break;
                }

            case CHAPTERBOX.lock:
                {
                    this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
                    this.schedule(() => {
                        this.box.play();
                    }, 2)
                    this.assetImpl.spriteAtlasFrame(this.boxImg, Res.texture.views.mainUi, 'unclaimed');
                    break;
                }

            case CHAPTERBOX.receive:
                {
                    let anim = this.boxImg.node.getComponent(cc.Animation);
                    anim.stop();
                    this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchStar0, this);
                    this.receive.active = true;
                    this.assetImpl.spriteAtlasFrame(this.boxImg, Res.texture.views.mainUi, 'unclaimed');
                    break;
                }
            default:
                break;
        }
    }

    /**领取宝箱奖励 */
    public receiveBox() {
        let chapterBoxArr = GameMgr.lUserData.chapterBoxArr;
        chapterBoxArr.push(this._data.id);
        GameMgr.lUserData.chapterBoxArr = chapterBoxArr;
        GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, this._data.sectionBox);
        GCtrl.ES.emit(CMsg.client.home.onaddLevel);
        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.mainBox, { msg: L(JXLocales.tower["mainBox" + this.curChapter], '+' + this._index) })
    }
} 